/*   
 * Copyright (c) 2010-2020 DMGIS Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * DMGIS. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with DMGIS.   
 *   
 */
package com.dmgis.qxfw.dataservice.bigdata.service.impl;


import com.dmgis.qxfw.dataservice.bigdata.service.ISpTourismService;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.tour_fcst;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.tour_stations;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.travel_predict;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.ForecastKey;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.SendInfo;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.SpFcstInfo;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.SpSmallFcstInfo;
import com.dmgis.qxfw.dataservice.businessservice.common.util.FileUtil;

import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.commonservice.utils.PGUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.github.kevinsawicki.http.HttpRequest;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("SpTourismServices")
public class SpTourismServiceImpl implements ISpTourismService {
	ProcessRequestController pc = new ProcessRequestController();

	@Override
	public ArrayList<SpFcstInfo> getSpecialFcstInfo(String date) {
		ArrayList<SpFcstInfo> result = new ArrayList<SpFcstInfo>();
		try {
			String strSql = String.format(
					"select stationID,type,typeName,suitable from special_fcst where dateTime = '%s' order by type,typeName",
					date);
			List<Map<String, Object>> resMaps = PGUtil.getResultSet2MapArr(DbConfig.pg_url + DbConfig.zyConn,
					DbConfig.pg_userName, DbConfig.pg_pwd, strSql);
			if (resMaps.size() > 0) {
				int count = 0;
				SpFcstInfo spInfo = new SpFcstInfo();
				spInfo.setSmallFcstInfo(new ArrayList<SpSmallFcstInfo>());
				String levelClass = resMaps.get(0).get("type").toString();// 用来记录分级值
				for (Map<String, Object> map : resMaps) {
					String bigClass = map.get("type").toString();
					SpSmallFcstInfo sInfo = new SpSmallFcstInfo();
					sInfo.setSmallClass(map.get("typename").toString());
					sInfo.setSuitableInfo(map.get("suitable").toString());
					sInfo.setStationId(map.get("stationid").toString());
					spInfo.setBigClass(bigClass);
					spInfo.getSmallFcstInfo().add(sInfo);
					count++;
					if (!levelClass.equals(bigClass))// 记录一个大类别的预测结果
					{
						spInfo.setBigClass(levelClass);
						spInfo.getSmallFcstInfo().add(sInfo);// 基本小类别预测结果
						result.add(spInfo);// 添加到结果中
						spInfo = new SpFcstInfo();// 重置大类别
						sInfo = new SpSmallFcstInfo();// 重置小类别
						levelClass = bigClass;// 记录分级数
					}
					if (count == resMaps.size()) {
						result.add(spInfo);
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	@Override
	public List<SendInfo> getDelicacy(String urlPath, String localPath) {
		List<SendInfo> result = new ArrayList<SendInfo>();
		try {
			String strSql = String
					.format("SELECT id,title,address,imagename,text,time  FROM Delicacy order by ID desc limit 10");
			result = PGUtil.getBeans(DbConfig.pg_url + DbConfig.zyConn, DbConfig.pg_userName, DbConfig.pg_pwd, strSql,
					SendInfo.class);
			for (SendInfo sendInfo : result) {
				String PhysicUrl = localPath + "delicacy" + sendInfo.getId() + ".png";// 本地路径
				String httpUrl = urlPath + "delicacy" + sendInfo.getId() + ".png";
				// 当图片上传url存在并且图片review url不存在时
				File file = new File(PhysicUrl);
				if (!file.exists()) {
					String downUrl = DbConfig.mongoSelectUrl + "?collectionName=image_file&table_name=delicacy&id="
							+ "delicacy" + sendInfo.getId();
					FileUtil.bytesToFile(HttpRequest.get(downUrl, true).bytes(), PhysicUrl);
				}
				sendInfo.setImageName(httpUrl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<SendInfo> getDelicacyByID(String id, String urlPath, String localPath) {
		List<SendInfo> result = new ArrayList<SendInfo>();
		try {
			String strSql = String.format(
					"SELECT id,title,address,imagename,text,time  FROM Delicacy where ID<%s order by ID desc limit 10",
					id);
			result = PGUtil.getBeans(DbConfig.pg_url + DbConfig.zyConn, DbConfig.pg_userName, DbConfig.pg_pwd, strSql,
					SendInfo.class);
			for (SendInfo sendInfo : result) {
				String PhysicUrl = localPath + "delicacy" + sendInfo.getId() + ".png";// 本地路径
				String httpUrl = urlPath + "delicacy" + sendInfo.getId() + ".png";
				// 当图片上传url存在并且图片review url不存在时
				File file = new File(PhysicUrl);
				if (!file.exists()) {
					String downUrl = DbConfig.mongoSelectUrl + "?collectionName=image_file&table_name=delicacy&id="
							+ "delicacy" + sendInfo.getId();
					FileUtil.bytesToFile(HttpRequest.get(downUrl, true).bytes(), PhysicUrl);
				}
				sendInfo.setImageName(httpUrl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<SendInfo> getDelicacyInfo(String id, String urlPath, String localPath) {
		List<SendInfo> result = new ArrayList<SendInfo>();
		try {
			String strSql = String.format("SELECT title,imagename,text FROM Delicacy where ID=%s", id);
			result = PGUtil.getBeans(DbConfig.pg_url + DbConfig.zyConn, DbConfig.pg_userName, DbConfig.pg_pwd, strSql,
					SendInfo.class);
			for (SendInfo sendInfo : result) {
				String PhysicUrl = localPath + "delicacy" + sendInfo.getId() + ".png";// 本地路径
				String httpUrl = urlPath + "delicacy" + sendInfo.getId() + ".png";
				// 当图片上传url存在并且图片review url不存在时
				File file = new File(PhysicUrl);
				if (!file.exists()) {
					String downUrl = DbConfig.mongoSelectUrl + "?collectionName=image_file&table_name=delicacy&id="
							+ "delicacy" + id;
					FileUtil.bytesToFile(HttpRequest.get(downUrl, true).bytes(), PhysicUrl);
				}
				sendInfo.setImageName(httpUrl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<SendInfo> getHotel(String urlPath, String localPath) {
		List<SendInfo> result = new ArrayList<SendInfo>();
		try {
			String strSql = String
					.format("SELECT id,title,address,imagename,text,time  FROM hotel order by ID desc limit 10");
			result = PGUtil.getBeans(DbConfig.pg_url + DbConfig.zyConn, DbConfig.pg_userName, DbConfig.pg_pwd, strSql,
					SendInfo.class);
			for (SendInfo sendInfo : result) {
				String PhysicUrl = localPath + "hotel" + sendInfo.getId() + ".png";// 本地路径
				String httpUrl = urlPath + "hotel" + sendInfo.getId() + ".png";
				// 当图片上传url存在并且图片review url不存在时
				File file = new File(PhysicUrl);
				if (!file.exists()) {
					String downUrl = DbConfig.mongoSelectUrl + "?collectionName=image_file&table_name=hotel&id="
							+ "hotel" + sendInfo.getId();
					FileUtil.bytesToFile(HttpRequest.get(downUrl, true).bytes(), PhysicUrl);
				}
				sendInfo.setImageName(httpUrl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<SendInfo> getHotelByID(String id, String urlPath, String localPath) {
		List<SendInfo> result = new ArrayList<SendInfo>();
		try {
			String strSql = String.format(
					"SELECT id,title,address,imagename,text,time  FROM hotel where ID<%s order by ID desc limit 10",
					id);
			result = PGUtil.getBeans(DbConfig.pg_url + DbConfig.zyConn, DbConfig.pg_userName, DbConfig.pg_pwd, strSql,
					SendInfo.class);
			for (SendInfo sendInfo : result) {
				String PhysicUrl = localPath + "hotel" + sendInfo.getId() + ".png";// 本地路径
				String httpUrl = urlPath + "hotel" + sendInfo.getId() + ".png";
				// 当图片上传url存在并且图片review url不存在时
				File file = new File(PhysicUrl);
				if (!file.exists()) {
					String downUrl = DbConfig.mongoSelectUrl + "?collectionName=image_file&table_name=hotel&id="
							+ "hotel" + sendInfo.getId();
					FileUtil.bytesToFile(HttpRequest.get(downUrl, true).bytes(), PhysicUrl);
				}
				sendInfo.setImageName(httpUrl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<SendInfo> getHotelInfo(String id, String urlPath, String localPath) {
		List<SendInfo> result = new ArrayList<SendInfo>();
		try {
			String strSql = String.format("SELECT title,imagename,text FROM hotel where ID=%s", id);
			result = PGUtil.getBeans(DbConfig.pg_url + DbConfig.zyConn, DbConfig.pg_userName, DbConfig.pg_pwd, strSql,
					SendInfo.class);
			for (SendInfo sendInfo : result) {
				String PhysicUrl = localPath + "hotel" + sendInfo.getId() + ".png";// 本地路径
				String httpUrl = urlPath + "hotel" + sendInfo.getId() + ".png";
				// 当图片上传url存在并且图片review url不存在时
				File file = new File(PhysicUrl);
				if (!file.exists()) {
					String downUrl = DbConfig.mongoSelectUrl + "?collectionName=image_file&table_name=hotel&id="
							+ "hotel" + id;
					FileUtil.bytesToFile(HttpRequest.get(downUrl, true).bytes(), PhysicUrl);
				}
				sendInfo.setImageName(httpUrl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Object getSpPathFcst(String date, String sc, String sx) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<travel_predict> getTourfcst(String string) {
		// TODO 获取特色旅游
		List<travel_predict> listData = new ArrayList<travel_predict>();
		String strSql = String.format("SELECT * FROM travel_predict where type = '" + string + "'");
		listData = PGUtil.getBeans(DbConfig.pg_url + DbConfig.zyConn, DbConfig.pg_userName, DbConfig.pg_pwd, strSql,
				travel_predict.class);
		return listData;
	}

	@Override
	public List<tour_fcst> getTourFcstInfo(ForecastKey selectKey) {
		// TODO 获取城市预报
		List<tour_fcst> resultList = new ArrayList<tour_fcst>();
		String strSql = String.format("SELECT * FROM tour_fcst where datechar = '" + selectKey.getDateChar()
				+ "' and timechar = '" + selectKey.getTimeChar() + "' and ntimes <= '" + selectKey.getNTimes()
				+ "' and stationname = '" + selectKey.getTourname() + "'");
		resultList = PGUtil.getBeans(DbConfig.pg_url + DbConfig.zyConn, DbConfig.pg_userName, DbConfig.pg_pwd, strSql,
				tour_fcst.class);
		return resultList;
	}

	@Override
	public Object getbestTourPath(String date) {
		return date;
		// TODO 最佳路线推荐
	}

	@Override
	public List<tour_stations> getTourSta(List<String> stationnames) {
		// TODO Auto-generated method stub
		List<tour_stations> resultList = new ArrayList<tour_stations>();
		String tempString = "";
		for (String station : stationnames) {
			tempString += String.format("'%s',", station);
		}
		tempString = tempString.substring(0, tempString.length() - 1);
		String strSql = String.format("SELECT * FROM tour_stations where stationname in (" + tempString + ")");
		resultList = PGUtil.getBeans(DbConfig.pg_url + DbConfig.zyConn, DbConfig.pg_userName, DbConfig.pg_pwd, strSql,
				tour_stations.class);
		return resultList;
	}

	@Override
	public List<travel_predict> getTourSta1(List<String> spotname) {
		// TODO Auto-generated method stub
		List<travel_predict> resultList = new ArrayList<travel_predict>();
		String tempString = "";
		try {
			for (String station : spotname) {
				tempString += String.format("'%s',", station);
			}
			tempString = tempString.substring(0, tempString.length() - 1);
			RequestDataBySelect dataBySelect=new RequestDataBySelect();
			dataBySelect.setParam("spotname in (" + tempString + ")");
			Map<String,String> map=new HashMap<String, String>();
			map.put("param", JsonUtil.object2Json(dataBySelect));
			map.put("method","travel_predict");
			map.put("sqlType", "select");
			String result = pc.processRequest(map);
			if (result.equals("")){
				result="[]";
			}
			resultList =JsonUtil.jsonStr2TypeReference(result, new TypeReference<List<travel_predict>>() {});
		}catch (Exception e	){
			e.printStackTrace();
		}
		return resultList;

	}
}
